//
//  QGHostSelectViewController.swift
//  B2BAutoziMall
//
//  Created by liangjie on 16/3/11.
//  Copyright © 2016年 qeegoo. All rights reserved.
//

import UIKit

open class APPHostViewController: BaseViewController {
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect(x:0, y:0, width:kScreenW, height:kScreenH))
        tableView.backgroundColor = BaseColor.BackGroundColor
        tableView.dataSource = self
        tableView.delegate = self
        return tableView
    }()

    var arrayData = [
        ["description":"内网","value":"http://192.168.11.21/mobile/"],
        ["description":"内网测试网","value":"http://m.b2bex.com"],
        ["description":"正式测试网络","value":"http://cfgj.b2bex.com/"],
        ["description":"发布网络","value":"http://api.zcpart.com"],
        ["description":"史慧鹏测试","value":"http://192.168.30.110:8090"],
        ["description":"张士欣","value":"http://106.39.30.220:8090"],
        ["description":"闫博测试","value":"http://192.168.20.29:8090"]
                     ]
    var stringAppHost :String = ""
    override open func viewDidLoad() {
        super.viewDidLoad()
        self.title = "App网址设定"
        baseSetNavLeftButtonIsBack()
        view.addSubview(tableView)
        self.stringAppHost = APP_HOST()
    }
}

extension APPHostViewController:UITableViewDataSource,UITableViewDelegate
{
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayData.count
    }
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil{
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
        }
        let dict = arrayData[indexPath.row]
        cell?.textLabel?.text = dict["description"]
        cell?.textLabel?.font = UIFont.systemFont(ofSize: 16)
        let host = dict["value"]
        cell?.detailTextLabel?.text = host
        cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 14)
        if self.stringAppHost == host {
            cell?.accessoryType = UITableViewCellAccessoryType.checkmark
        }else{
            cell?.accessoryType = UITableViewCellAccessoryType.none
        }
        return cell!
    }
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
        let dict = arrayData[indexPath.row]
        self.stringAppHost = dict["value"]!
        UserDefaults.standard.set(self.stringAppHost, forKey: __kAppHost_save)
        UserDefaults.standard.synchronize()
        _ = navigationController?.popViewController(animated: true)
    }
}

